import axios from 'axios';

const apiClient = axios.create({
  baseURL: 'http://localhost:8000/api/v1', // Assuming the backend runs on port 8000
  headers: {
    'Content-Type': 'application/json',
  },
});

/**
 * Sends recipe text to the backend for parsing.
 * @param {string} text The raw text describing the recipe.
 * @returns {Promise<object>} The parsed recipe data.
 */
export const parseRecipe = (text) => {
  return apiClient.post('/parse-recipe', { text });
};

/**
 * Sends the structured recipe data to be saved in the database.
 * @param {object} recipeData The structured recipe object.
 * @returns {Promise<object>} The saved recipe data including its new ID.
 */
export const createRecipe = (recipeData) => {
  return apiClient.post('/recipes', recipeData);
};

/**
 * Fetches all recipes from the database.
 * @returns {Promise<Array<object>>} A list of all recipes.
 */
export const getRecipes = () => {
  return apiClient.get('/recipes');
};

/**
 * Fetches an upload token from the backend.
 * @returns {Promise<object>} An object containing the token and domain.
 */
export const getUploadToken = () => {
  return apiClient.get('/storage/upload-token');
};

/**
 * Deletes a recipe by its ID.
 * @param {number} recipeId The ID of the recipe to delete.
 * @returns {Promise<object>}
 */
export const deleteRecipe = (recipeId) => {
  return apiClient.delete(`/recipes/${recipeId}`);
};

/**
 * Updates a recipe by its ID.
 * @param {number} recipeId The ID of the recipe to update.
 * @param {object} recipeData The new data for the recipe.
 * @returns {Promise<object>} The updated recipe data.
 */
export const updateRecipe = (recipeId, recipeData) => {
  return apiClient.put(`/recipes/${recipeId}`, recipeData);
};
